package network.message;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.Socket;

/**
 *
 */
public class SocketMessageSender implements IMessageSender {

    private Socket connection;

    public SocketMessageSender(Socket connection) {
        this.connection = connection;
    }

    public boolean sendMessage(String message) {
        BufferedOutputStream bufferedOutputStream = null;
        //Trying to create output stream.
        try {
            bufferedOutputStream = new BufferedOutputStream(connection.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }

        //Creating output writer and trying to write message.
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(bufferedOutputStream);
        try {
            outputStreamWriter.write(message);
            outputStreamWriter.write(13);
        } catch (IOException e) {
            e.printStackTrace();
            //TODO: smth useful
        }

        // Flushing in the end:
        try {
            outputStreamWriter.flush();
        } catch (IOException e) {
            e.printStackTrace();
            //TODO: smth useful
        }
        return true;
    }
}